package com.wstx.mediaserver.service;

import cn.hutool.http.HttpUtil;
import cn.hutool.system.SystemUtil;
import com.alibaba.fastjson.JSON;
import com.wstx.mediaserver.common.AppVar;
import com.wstx.mediaserver.domain.dto.RawVideoInfo;
import com.wstx.mediaserver.domain.dto.VideoInfo;
import com.wstx.mediaserver.util.Prt;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.SystemUtils;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class RecordService {

    final AppVar appVar;

    public void handleCallback(RawVideoInfo rawInfo) {
        VideoInfo videoInfo = new VideoInfo();
        videoInfo.setVehicle(rawInfo.getStream());
        String rawFileName = rawInfo.getFile();
        videoInfo.setDate(rawFileName.substring(rawFileName.indexOf('-') + 1, rawFileName.indexOf('.')));
        String fileName = rawFileName.substring(rawFileName.lastIndexOf('/') + 1);
        videoInfo.setFile(fileName);

        String ip = SystemUtils.IS_OS_WINDOWS ? appVar.vmIp : appVar.ip;

        videoInfo.setUrl("http://" + ip + ":" + appVar.recordProxyPort
                + "/" + rawInfo.getStream() + "/" + fileName);
        HttpUtil.post("http://localhost:8840/ms/ayaka/videoRecordMsg",
                JSON.toJSONString(videoInfo));
    }

    public static void main(String[] args) {
        String rawFileName = "/tsp/output/video/wstx/vehicle01/vehicle01-20220210175724.flv";
        String date = rawFileName.substring(rawFileName.indexOf('-') + 1, rawFileName.indexOf('.'));
        String file = rawFileName.substring(rawFileName.lastIndexOf('/') + 1);

        Prt.p(date, file);
    }
}
